package com.penghk.hangzhouhouseprice.service;

import com.penghk.hangzhouhouseprice.dao.HousePriceRepositpry;
import com.penghk.hangzhouhouseprice.entity.HousePrice;
import com.penghk.hangzhouhouseprice.handler.LianjiaSecondHousePriceHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class LianjiaHousePriceService {
    private Logger logger = LoggerFactory.getLogger(LianjiaHousePriceService.class);

    private static final String saveFromNet = "LianjiaHousePriceService#saveFromNet";

    @Autowired
    HousePriceRepositpry housePriceRepositpry;

    @Autowired
    LianjiaSecondHousePriceHandler lianjiaSecondHousePriceHandler;

    public List<HousePrice> findByArea(String area) {
        return housePriceRepositpry.findByArea(area);
    }

    public void save(HousePrice housePrice) {
        housePriceRepositpry.save(housePrice);
    }

    public void saveAll(Iterable<HousePrice> housePrices) {
        housePriceRepositpry.save(housePrices);
    }

    public void saveFromNet() {
        List<HousePrice> housePriceList = lianjiaSecondHousePriceHandler.getAllHousePrice();
        if (!CollectionUtils.isEmpty(housePriceList)) {
            saveAll(housePriceList);
            logger.info("{} 保存所有房价信息成功" , saveFromNet);
        }
    }

}
